package com.j2203.controller;

import cn.hutool.json.JSONUtil;
import com.j2203.entity.Comment;
import com.j2203.entity.Game;
import com.j2203.entity.Gmdetail;
import com.j2203.service.ShoppingCartService;
import com.j2203.vo.ShoppingCart;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class ShoppingCartController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Resource(type = ShoppingCartService.class)
    private ShoppingCartService shoppingCartService;


    /**
     * 查询单个点击详情--游戏详情
     * @param gid
     * @return
     */
    @GetMapping("/findByIdGame")
    public Game findByIdGame(@RequestParam(value = "gid") int gid){
        Game byIdGame = shoppingCartService.findByIdGame(gid);
        return byIdGame;
    }

    /**
     * 游戏详情1-3
     * @param gid
     * @return
     */
    @GetMapping("/findByIdgmdetail")
    public Gmdetail findByIdgmdetail(@RequestParam(value = "gid") int gid){
        Gmdetail byIdgmdetail = shoppingCartService.findByIdgmdetail(gid);
        return byIdgmdetail;
    }


    /**
     * 游戏评论
     * @param gid
     * @return
     */
    @GetMapping("/findByIdComment")
    public List<Comment> findByIdComment(@RequestParam(value = "gid") int gid){
        List<Comment> allComment = shoppingCartService.findAllComment(gid);
        return allComment;
    }


    /**
     * 添加购物车+查看购物车
     * @return
     */
    @GetMapping("/addShoppingCar")
    public ShoppingCart addShoppingCar(@RequestParam(value = "gid",defaultValue = "0")int gid,int uid){
        ShoppingCart shoppingCart = shoppingCartService.addShoppingCar(gid, uid);
        return shoppingCart;
    }

    /**
     * 删除单条购物车记录
     * @param gid
     * @param uid
     * @return
     */
    @GetMapping("/delByIdShoppingCar")
    public int delByIdShoppingCar(@RequestParam(value = "gid")int gid,@RequestParam(value = "uid")int uid){
        int i = shoppingCartService.delByIdShoppingCar(gid, uid);
        return i;
    }


    /**
     * 清空购物车
     * @param uid
     * @return
     */
    @GetMapping("/delAllShoppingCar")
    public int delAllShoppingCar(@RequestParam(value = "uid")int uid){
        int i = shoppingCartService.delAllShoppingCar(uid);
        return i;
    }

    /**
     * 点击购买写订单展示订单
     * @param uid
     * @return
     */
    @GetMapping("/addOrder")
    public ShoppingCart addOrder(@RequestParam(value = "uid")int uid){
        ShoppingCart shoppingCart = shoppingCartService.addOrder(uid);
        return shoppingCart;
    }


    @GetMapping("/updateOrder")
    public int updateOrder(){
        //此处uid应该从redis中获得
        int uid = 1;
        String s = stringRedisTemplate.opsForValue().get(uid + "");
        Long aLong = JSONUtil.toBean(s, Long.class);
        int i = shoppingCartService.updateOrder(aLong);
        return i;
    }

}
